/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects.smartidlc.model;


public class IDLDataType {
	public enum DataTypeId {
		PRIMITIVE,
		COMPLEX,
		QUALIFIED_NAME
	}	

	private String name;
	private DataTypeId typeId;
	
	private IDLPrimitiveType primitiveType = null;
	private IDLComplexType complexType = null;
	private Object userObject = null;
	private boolean encrypted = false;
	private boolean required = false;
	
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}	
	
	public void setPrimitiveType(IDLPrimitiveType primitiveType) {
		this.primitiveType = primitiveType;
	}
	public IDLPrimitiveType getPrimitiveType() {
		return primitiveType;
	}
			
	public void setComplexType(IDLComplexType complexType) {
		this.complexType = complexType;
	}
	public IDLComplexType getComplexType() {
		return complexType;
	}

	public void setTypeId(DataTypeId typeId) {
		this.typeId = typeId;
	}
	public DataTypeId getTypeId() {
		return typeId;
	}
	public void setUserObject(Object userObject) {
		this.userObject = userObject;
	}
	public Object getUserObject() {
		return userObject;
	}
	
	public void setEncrypted(boolean encrypted) {
		this.encrypted = encrypted;
	}
	public boolean isEncrypted() {
		return encrypted;
	}
	public boolean isImported() {
		if ( typeId == DataTypeId.QUALIFIED_NAME ) {
			if ( name.matches(".+\\..+")) {
				return true;
			}
		}
		return false;
	}
	
	public String getRelativeName() {
		String[] parts = this.name.split("\\.");
		if (parts.length > 0) {
			return parts[parts.length-1];
		}
		else
			return this.name;
	}
	
	public boolean isEqualsToType ( IDLDataType srcType) {
		boolean result = false;
		if ( srcType.getTypeId().equals(this.getTypeId()) ) {
			if ( this.getTypeId().equals(DataTypeId.PRIMITIVE) ) {
				if ( srcType.getPrimitiveType().getTypeId().equals(
						this.getPrimitiveType().getTypeId()) ) {
					result = true;
				}
			}
			if ( this.getTypeId().equals(DataTypeId.COMPLEX)) {
				if ( srcType.getComplexType().getComplexTypeId().equals(
						this.getComplexType().getComplexTypeId()) ) {
					if ( this.getComplexType().getComplexItemDataType().equals(
							srcType.getComplexType().getComplexItemDataType()) ) {
						if ( this.getComplexType().getComplexTypeId().equals(IDLComplexType.ComplexTypeId.MAP)) {
							if ( this.getComplexType().getComplexKeyDataType().equals(
									srcType.getComplexType().getComplexKeyDataType()) ) {
								result= true;
							}
						}
						else
							result = true;
					}
				}
			}
			if ( this.getTypeId().equals(DataTypeId.QUALIFIED_NAME)) {
				result = this.getUserObject().equals(srcType.getUserObject());
			}
		}	
		return result;
	}
	public void setRequired(boolean required) {
		this.required = required;
	}
	public boolean isRequired() {
		return required;
	}


}
